package com.erp.app.common.select.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

import com.erp.app.common.xls.vo.XlsSqlVo;
import com.erp.framework.exceptions.CreateRecordException;
import com.erp.framework.exceptions.DeleteRecordException;
import com.erp.framework.exceptions.FindRecordException;
import com.erp.framework.exceptions.ModifyRecordException;
import com.erp.framework.model.dao.SimpleBaseDao;
import com.erp.framework.model.vo.ChildList;
import com.erp.framework.model.vo.SimpleBaseVo;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.DbObjectUtil;
import com.erp.framework.util.logger.Logger;

public class XlsDao extends SimpleBaseDao {
	 public HashMap selectXlsMap(XlsSqlVo xvo, Connection conn) throws FindRecordException {
	    	HashMap result = new HashMap();
	        Logger.sql(this, xvo.getSqlXlsSql());
	        PreparedStatement statement = null;
	        ResultSet rs = null;
	        DbObjectUtil util = new DbObjectUtil();
	        try {
	        	ResultSetMetaData md = null;
	            statement = conn.prepareStatement(xvo.getSqlXlsSql());
	            rs = statement.executeQuery();
	            md = rs.getMetaData();
	            int noCols = md.getColumnCount();
	            if (rs != null) {
	                while ((rs != null) && (rs.next())) {
	                    for (int i = 1; i < (noCols + 1); i++) {
	                        int type = md.getColumnType(i);
	                        String colName = md.getColumnName(i);
	                        Object obj = null;
	                        switch (type) {
	                            case java.sql.Types.BIGINT:
	                                obj = rs.getBigDecimal(i);
	                                break;
	                            case java.sql.Types.CHAR:
	                                obj = rs.getString(i);
	                                break;
	                            case java.sql.Types.DATE:
	                                obj = rs.getDate(i);
	                                break;
	                            case java.sql.Types.DECIMAL:
	                                obj = rs.getBigDecimal(i);
	                                break;
	                            case java.sql.Types.DOUBLE:
	                                obj = new Double(rs.getDouble(i));
	                                break;
	                            case java.sql.Types.FLOAT:
	                                obj = new Float(rs.getFloat(i));
	                                break;
	                            case java.sql.Types.INTEGER:
	                                obj = new Integer(rs.getInt(i));
	                                break;
	                            case java.sql.Types.NUMERIC:
	                                obj = rs.getBigDecimal(i);
	                                break;
	                            case java.sql.Types.TIMESTAMP:
	                                obj = rs.getTimestamp(i);
	                                break;
	                            case java.sql.Types.REAL:
	                                obj = rs.getBigDecimal(i);
	                                break;
	                            case java.sql.Types.VARCHAR:
	                                obj = rs.getString(i);
	                                break;
	                            default:
	                            	obj = "";
	                            	break;
	                        }
//	                        System.out.println("colName==========================" + colName);
//	                        System.out.println("obj==========================" + obj);
	                        if (obj != null) {
	                        	result.put(colName, obj);
	                        } else {
	                        	result.put(colName, "");
	                        }
	                    }
	                }
	            }
	            
	        } catch (SQLException e) {
	            Logger.error(this, e);
	            throw new FindRecordException(e.getMessage());
	        } finally {
	            try {
	                if (rs != null) rs.close();
	                if (statement != null) statement.close();
	            } catch (SQLException e) {}
	        }
	        return result;
	    }
	    
	 public HashMap selectXlsDtlMap(XlsSqlVo xvo, Connection conn) {
	    	HashMap re = new HashMap();
	    	HashMap colNameMap = new HashMap();
	        ArrayList rslt = new ArrayList();
	        ArrayList valueList = new ArrayList();
	        Statement stmt = null;
	        ResultSet rs = null;
	        ResultSetMetaData md = null;

	        Logger.sql(this, "selectList(), sql=" + xvo.getSqlXlsSql());

	        try {
	            stmt = conn.createStatement();
	            rs = stmt.executeQuery(xvo.getSqlXlsSql());
	            md = rs.getMetaData();
	            int noCols = md.getColumnCount();
	            int flag=0;
	            if (rs != null) {
	                while ((rs != null) && (rs.next())) {
	                	flag++;
	                    for (int i = 1; i < (noCols + 1); i++) {
	                        int type = md.getColumnType(i);
	                        String colName = md.getColumnName(i);
	                        if(flag==1)colNameMap.put(colName, String.valueOf(i-1));
	                        
	                        Object obj = null;

	                        switch (type) {
	                        case java.sql.Types.BIGINT:
                                obj = rs.getBigDecimal(i);
                                break;
                            case java.sql.Types.CHAR:
                                obj = rs.getString(i);
                                break;
                            case java.sql.Types.DATE:
                                obj = rs.getDate(i);
                                break;
                            case java.sql.Types.DECIMAL:
                                obj = rs.getBigDecimal(i);
                                break;
                            case java.sql.Types.DOUBLE:
                                obj = new Double(rs.getDouble(i));
                                break;
                            case java.sql.Types.FLOAT:
                                obj = new Float(rs.getFloat(i));
                                break;
                            case java.sql.Types.INTEGER:
                                obj = new Integer(rs.getInt(i));
                                break;
                            case java.sql.Types.NUMERIC:
                                obj = rs.getBigDecimal(i);
                                break;
                            case java.sql.Types.TIMESTAMP:
                                obj = rs.getTimestamp(i);
                                break;
                            case java.sql.Types.REAL:
                                obj = rs.getBigDecimal(i);
                                break;
                            case java.sql.Types.VARCHAR:
                                obj = rs.getString(i);
                                break;
                            default:
                            	obj = "";
                            	break;
	                        }

	                        if (obj != null) {
	                        	valueList.add(obj);
	                        } else {
	                        	valueList.add(null);
	                        }
	                    }
	                    rslt.add(valueList);
	                    valueList = new ArrayList();
	                }
	            }
	        } catch (Exception e) {
	            Logger.logExceptions(this, e);
	        } finally {
	            try {
	                if (rs != null)
	                    rs.close();
	                if (stmt != null)
	                    stmt.close();
	            } catch (Exception e) {
	            	Logger.logExceptions(this, e);
	            }
	        }
	        re.put("NAME" , colNameMap);
	        re.put("VALUE" , rslt);
	        return re;
	    }

	public SimpleBaseVo findByPrimaryKey(Object obj, Connection conn) throws FindRecordException {
		// TODO Auto-generated method stub
		return null;
	}

	public ChildList selectByParent(SimpleBaseVo parent, Connection conn) throws FindRecordException {
		// TODO Auto-generated method stub
		return null;
	}

	public void insert(SimpleBaseVo bizModel, Connection conn) throws CreateRecordException {
		// TODO Auto-generated method stub
		
	}

	public void update(SimpleBaseVo bizModel, Connection conn) throws ModifyRecordException {
		// TODO Auto-generated method stub
		
	}

	public void remove(SimpleBaseVo bizModel, Connection conn) throws DeleteRecordException {
		// TODO Auto-generated method stub
		
	}
}
